package com.ag.lwc.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.ag.bean.TbEvent;
import com.ag.bean.TbEventCleanup;
import com.ag.bean.TbEventCleanupAttach;
import com.ag.bean.TbUser;
import com.ag.common.BaseAction;
import com.ag.common.IPermissionService;
import com.ag.lwc.service.EventCleanupServise;
import com.ag.lzm.utils.JacksonMapper;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author user
 *
 */
public class EventCleanupAction extends BaseAction implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -9160670317278137260L;
	private EventCleanupServise service;
	private List<TbEventCleanup> result = null;				//事件善后列表,查询结果
	private String eventId;											//事件编号,查询用
    private String taName;										//附件名称,增加用
    private String taAddress;									//附件路径,增加用
    private TbEventCleanup eventCleanup;						//事件善后，供增加用
    private String message;
    private String files;

    private String originFiles;

	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;
	private Map<String, Object> sessionMap;

    
   /**
    * 查找事件善后
    * @return
    */
    public String find()
    {try{
    	System.out.println("action： 事件编号"+eventId);
    		result = service.findCleanupByEventId(eventId);
     	}catch(Exception e){e.printStackTrace();}
    	
    	message ="findsuccess";
    	return "find";
    }
    /**
     * 添加事件善后
     * @return
     */
    public String add()
    {	
    	//获取上传的文件
    	System.out.println(files);
    	List<String> fileStrings = new ArrayList<String>();//文件名List
    	List<String> originFileStrings = new ArrayList<String>();
    	ObjectMapper mapper = JacksonMapper.getInstance();
		try {
			JsonNode jsonFileNode = mapper.readValue(files, JsonNode.class);
			Iterator<JsonNode> fileNodes = jsonFileNode.getElements();
			while (fileNodes.hasNext()) {
				String node = fileNodes.next().getTextValue();
				System.out.println(node);
				fileStrings.add(node);
			}
			
			JsonNode jsonOriginFileNode = mapper.readValue(files, JsonNode.class);
			Iterator<JsonNode> originFileNodes = jsonOriginFileNode.getElements();
			while (originFileNodes.hasNext()) {
				String node = originFileNodes.next().getTextValue();
				System.out.println(node);
				originFileStrings.add(node);
			}

		} catch (JsonParseException e1) {
	
			e1.printStackTrace();
		} catch (JsonMappingException e1) {
		
			e1.printStackTrace();
		} catch (IOException e1) {
		
			e1.printStackTrace();
		}
		
		//加入到数据库		
		try{
		System.out.println("添加的事件善后附件："+fileStrings);
    	System.out.println("增加事件时候时: 事件善后内容："+eventCleanup.getEcContent());
    	TbEventCleanupAttach cleanupAttach = new TbEventCleanupAttach();	//附件
    	if((taAddress!=null)&&(taName!=null))
    		{
    		 cleanupAttach.setTaAddress(taAddress);
    		 cleanupAttach.setTaName(taName);
    		 cleanupAttach.setTbEventCleanup(eventCleanup);
    		 
    		}
    	else cleanupAttach=null;
    	
    	
    	TbEvent event = new TbEvent();
    	event.setEId(eventId);
    	eventCleanup.setTbEvent(event);				//事件善后的当前事件
    	eventCleanup.setEcTime(new Date());    	
    		//保存事件善后及其附件
    		eventCleanup.setTbUser(this.currentUser());			//当前用户
    		List<TbEventCleanupAttach> attachs = new LinkedList<TbEventCleanupAttach>();
    		
    		//保存文件名
    		for(String filename:fileStrings)
    		{ 
    			ActionContext ac = ActionContext.getContext();
    			ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
    			String savePath = sc.getRealPath("/");
    			savePath = savePath + "uploads\\";
    			TbEventCleanupAttach attach= new TbEventCleanupAttach();
    			attach.setTaAddress(savePath+filename);						//附件文件路径
    			attach.setTaName(filename);							//附件文件名
    			System.out.println("附件文件名："+filename+"   附件文件路径："+attach.getTaAddress());
    			attach.setTbEventCleanup(eventCleanup);
    			attachs.add(attach);
    		}
    		//eventCleanup.setTbEventCleanupAttachs(attachs);
    		service.addTbEventCleanup(eventCleanup, cleanupAttach);
    		message="addSuccess";
    	}catch(Exception e){
    		e.printStackTrace();
    		message="addFail";
    	}
    	
    	return "add";
    }
	public EventCleanupServise getService() {
		return service;
	}
	public void setService(EventCleanupServise service) {
		this.service = service;
	}
	public List<TbEventCleanup> getResult() {
		return result;
	}
	public void setResult(List<TbEventCleanup> result) {
		this.result = result;
	}
	public String getEventId() {
		return eventId;
	}
	public void setEventId(String eventId) {
		this.eventId = eventId;
	}
	public String getTaName() {
		return taName;
	}
	public void setTaName(String taName) {
		this.taName = taName;
	}
	public String getTaAddress() {
		return taAddress;
	}
	public void setTaAddress(String taAddress) {
		this.taAddress = taAddress;
	}
	public TbEventCleanup getEventCleanup() {
		return eventCleanup;
	}
	public void setEventCleanup(TbEventCleanup eventCleanup) {
		this.eventCleanup = eventCleanup;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
		
	}
	public String getFiles() {
		return files;
	}
	public void setFiles(String files) {
		this.files = files;
	}
	@Override
	public void setSession(Map<String, Object> arg0) {
		this.sessionMap = arg0;
		
	}
	public TbUser currentUser()					//返回当前用户的id
	{
		TbUser	user = pms.getCurrentUser(sessionMap);			//获取当前用户的id
		  System.out.println("当前用户的id："+user.getUId());
		return user;
	}
	public String getOriginFiles() {
		return originFiles;
	}
	public void setOriginFiles(String originFiles) {
		this.originFiles = originFiles;
	}
    
}
